Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 330)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.52694 11.56602 11.60474 11.64310 11.68109 11.71875 11.75606 11.79303
##   [9] 11.82969 11.86602 11.90205 11.93778 11.97321 12.00834 12.04318 12.07769
##  [17] 12.11188 12.14573 12.17922 12.21235 12.24511 12.27748 12.30966 12.34183
##  [25] 12.37390 12.40580 12.43747 12.46882 12.49979 12.53030 12.56028 12.58966
##  [33] 12.61836 12.64632 12.67466 12.70419 12.73439 12.76471 12.79461 12.82356
##  [41] 12.85101 12.87642 12.89927 12.92147 12.94502 12.96941 12.99413 13.01869
##  [49] 13.04258 13.06531 13.08638 13.10528 13.12152 13.13459 13.14400 13.15148
##  [57] 13.15883 13.16557 13.17124 13.17539 13.17755 13.17726 13.17407 13.16752
##  [65] 13.15639 13.14027 13.11988 13.09594 13.06916 13.04025 13.00993 12.97892
##  [73] 12.94794 12.91769 12.88890 12.86229 12.83205 12.79399 12.75116 12.70665
##  [81] 12.66351 12.62482 12.59364 12.56566 12.53509 12.50289 12.46999 12.43731
##  [89] 12.40580 12.37638 12.34999 12.32757 12.30839 12.29098 12.27519 12.26084
##  [97] 12.24775 12.23577 12.22471 12.21440 12.20469 12.19539 12.18633 12.17735
## [105] 12.16929 12.16293 12.15800 12.15425 12.15142 12.14924 12.14744 12.14577
## [113] 12.14397 12.14305 12.14390 12.14603 12.14899 12.15229 12.15547 12.15806
## [121] 12.16099 12.16535 12.17088 12.17735 12.18450 12.19210 12.19988 12.20761
## [129] 12.21504 12.22191 12.22800 12.23303 12.23837 12.24519 12.25300 12.26134
## [137] 12.26973 12.27768 12.28471 12.29036 12.29414 12.29530 12.29378 12.29008
## [145] 12.28473 12.27826 12.27118 12.26400 12.25727 12.25148 12.24717 12.24485
## [153] 12.24505 12.24828 12.25506 12.26202 12.26637 12.26957 12.27306 12.27828
## [161] 12.28669 12.29973 12.31793 12.34039 12.36637 12.39514 12.42596 12.45810
## [169] 12.49083 12.52341 12.55510 12.58518 12.61291 12.63755 12.66386 12.69636
## [177] 12.73400 12.77571 12.82044 12.86713 12.91472 12.96214 13.00833 13.05225
## [185] 13.09283 13.12900 13.15972 13.18391 13.20052 13.20850 13.20935 13.20562
## [193] 13.19775 13.18619 13.17140 13.15382 13.13392 13.11214 13.08893 13.06475
## [201] 13.04004 13.01527 12.98456 12.94346 12.89451 12.84026 12.78326 12.72605
## [209] 12.67118 12.62120 12.57866 12.53900 12.49638 12.45139 12.40464 12.35672
## [217] 12.30824 12.25979 12.21198 12.16541 12.12067 12.07837 12.03911 12.00108
## [225] 11.96237 11.92339 11.88459 11.84638 11.80919 11.77345 11.73959 11.70803
## [233] 11.67731 11.64592 11.61425 11.58269 11.55163 11.52146 11.49256 11.46534
## [241] 11.44018 11.41747 11.39760 11.38095 11.36626 11.35228 11.33942 11.32811
## [249] 11.31876 11.31180 11.30765 11.30554 11.30454 11.30485 11.30665 11.31014
## [257] 11.31550 11.32292 11.33259 11.34471 11.36223 11.38711 11.41802 11.45361
## [265] 11.49256 11.53352 11.57516 11.61615 11.65514 11.69081 11.72181 11.74682
## [273] 11.77124 11.80039 11.83276 11.86682 11.90106 11.93397 11.96404 11.98976
## [281] 12.00960 12.02393 12.03488 12.04357 12.05115 12.05873 12.06745 12.07843
## [289] 12.08922 12.09702 12.10246 12.10616 12.10875 12.11087 12.11314 12.11620
## [297] 12.12067 12.12717 12.13635 12.14882 12.16290 12.17656 12.18995 12.20324
## [305] 12.21658 12.23011 12.24400 12.25839 12.27345 12.28932 12.30616 12.32413
## [313] 12.34337 12.36405 12.38565 12.40763 12.43009 12.45313 12.47684 12.50131
## [321] 12.52664 12.55293 12.58027 12.60889 12.63884 12.66997 12.70214 12.73518
## [329] 12.76896 12.80332
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 330)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.71815 10.81037 10.90074 10.98924 11.07585 11.16057 11.24337 11.32424
##   [9] 11.40317 11.48014 11.55514 11.62815 11.69916 11.76814 11.83510 11.90009
##  [17] 11.96313 12.02427 12.08353 12.14096 12.19659 12.25046 12.30229 12.35183
##  [25] 12.39918 12.44443 12.48768 12.52902 12.56854 12.60633 12.64249 12.67712
##  [33] 12.71030 12.74213 12.77198 12.79934 12.82446 12.84758 12.86897 12.88887
##  [41] 12.90753 12.92520 12.94215 12.95668 12.96732 12.97459 12.97902 12.98114
##  [49] 12.98148 12.98056 12.97893 12.97711 12.97563 12.97501 12.97579 12.97648
##  [57] 12.97548 12.97317 12.96990 12.96606 12.96200 12.95811 12.95475 12.95230
##  [65] 12.95054 12.94894 12.94733 12.94555 12.94346 12.94088 12.93766 12.93365
##  [73] 12.92868 12.92260 12.91525 12.90646 12.89694 12.88731 12.87736 12.86686
##  [81] 12.85559 12.84332 12.82983 12.81485 12.79844 12.78092 12.76256 12.74366
##  [89] 12.72452 12.70543 12.68668 12.66857 12.65087 12.63305 12.61498 12.59650
##  [97] 12.57748 12.55777 12.53723 12.51571 12.49308 12.46919 12.44389 12.41705
## [105] 12.38446 12.34366 12.29713 12.24733 12.19674 12.14782 12.10304 12.06487
## [113] 12.03579 12.00712 11.97103 11.93124 11.89146 11.85541 11.82681 11.80936
## [121] 11.80019 11.79379 11.78993 11.78842 11.78903 11.79155 11.79577 11.80146
## [129] 11.80843 11.81644 11.82530 11.83477 11.84806 11.86750 11.89163 11.91899
## [137] 11.94811 11.97753 12.00579 12.03142 12.05296 12.07456 12.10090 12.13124
## [145] 12.16489 12.20112 12.23922 12.27848 12.31818 12.35761 12.39605 12.43280
## [153] 12.46713 12.49833 12.52570 12.54889 12.56885 12.58674 12.60372 12.62095
## [161] 12.63959 12.66083 12.68322 12.70467 12.72529 12.74519 12.76450 12.78334
## [169] 12.80181 12.82005 12.83816 12.85627 12.87449 12.89295 12.91363 12.93802
## [177] 12.96548 12.99535 13.02700 13.05976 13.09299 13.12605 13.15829 13.18905
## [185] 13.21770 13.24357 13.26604 13.28444 13.29813 13.30646 13.31102 13.31383
## [193] 13.31488 13.31416 13.31167 13.30739 13.30133 13.29347 13.28381 13.27234
## [201] 13.25906 13.24395 13.22629 13.20563 13.18234 13.15682 13.12946 13.10065
## [209] 13.07077 13.04022 13.00938 12.97626 12.93904 12.89833 12.85475 12.80893
## [217] 12.76149 12.71306 12.66426 12.61571 12.56804 12.52187 12.47782 12.43093
## [225] 12.37720 12.31872 12.25758 12.19585 12.13565 12.07903 12.02811 11.98496
## [233] 11.94649 11.90832 11.87059 11.83345 11.79705 11.76153 11.72705 11.69374
## [241] 11.66177 11.63126 11.60238 11.57526 11.54925 11.52383 11.49941 11.47640
## [249] 11.45519 11.43618 11.41979 11.40597 11.39433 11.38470 11.37692 11.37085
## [257] 11.36632 11.36318 11.36127 11.36043 11.36135 11.36465 11.37012 11.37752
## [265] 11.38665 11.39726 11.40915 11.42209 11.43585 11.45021 11.46495 11.47985
## [273] 11.49765 11.52043 11.54697 11.57604 11.60641 11.63686 11.66616 11.69309
## [281] 11.71642 11.74093 11.77087 11.80422 11.83895 11.87302 11.90442 11.93111
## [289] 11.95466 11.97800 12.00114 12.02410 12.04690 12.06955 12.09208 12.11449
## [297] 12.13681 12.15904 12.18122 12.20334 12.22533 12.24705 12.26851 12.28970
## [305] 12.31063 12.33129 12.35168 12.37179 12.39163 12.41118 12.43045 12.44944
## [313] 12.46814 12.48655 12.50465 12.52243 12.53993 12.55715 12.57414 12.59090
## [321] 12.60747 12.62387 12.64012 12.65623 12.67220 12.68799 12.70359 12.71897
## [329] 12.73410 12.74898
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 330)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.82863 10.89294 10.95608 11.01801 11.07871 11.13815 11.19631 11.25314
##   [9] 11.30863 11.36273 11.41544 11.46671 11.51652 11.56489 11.61191 11.65762
##  [17] 11.70204 11.74523 11.78721 11.82802 11.86771 11.90632 11.94321 11.97788
##  [25] 12.01055 12.04144 12.07078 12.09877 12.12563 12.15159 12.17686 12.20166
##  [33] 12.22620 12.25072 12.27465 12.29738 12.31907 12.33987 12.35991 12.37936
##  [41] 12.39835 12.41704 12.43558 12.45479 12.47515 12.49626 12.51772 12.53912
##  [49] 12.56009 12.58021 12.59908 12.61632 12.63152 12.64428 12.65422 12.66159
##  [57] 12.66708 12.67086 12.67314 12.67409 12.67391 12.67278 12.67090 12.66845
##  [65] 12.66488 12.65965 12.65290 12.64480 12.63551 12.62519 12.61399 12.60208
##  [73] 12.58962 12.57676 12.56366 12.55049 12.53512 12.51608 12.49452 12.47159
##  [81] 12.44846 12.42626 12.40616 12.38501 12.35974 12.33156 12.30172 12.27142
##  [89] 12.24191 12.21440 12.19012 12.17031 12.15323 12.13638 12.11988 12.10380
##  [97] 12.08825 12.07332 12.05910 12.04569 12.03318 12.02167 12.01124 12.00200
## [105] 11.99492 11.99047 11.98796 11.98671 11.98603 11.98524 11.98365 11.98058
## [113] 11.97535 11.96941 11.96446 11.96023 11.95641 11.95272 11.94886 11.94455
## [121] 11.94091 11.93906 11.93868 11.93942 11.94097 11.94299 11.94516 11.94714
## [129] 11.94861 11.94925 11.94871 11.94667 11.94216 11.93491 11.92566 11.91515
## [137] 11.90411 11.89328 11.88340 11.87522 11.86946 11.86358 11.85495 11.84419
## [145] 11.83193 11.81882 11.80547 11.79251 11.78057 11.77029 11.76229 11.75721
## [153] 11.75567 11.75829 11.76572 11.77647 11.78860 11.80210 11.81693 11.83306
## [161] 11.85047 11.86912 11.89060 11.91607 11.94488 11.97638 12.00991 12.04481
## [169] 12.08045 12.11615 12.15127 12.18516 12.21716 12.24662 12.27818 12.31623
## [177] 12.35975 12.40769 12.45902 12.51272 12.56775 12.62307 12.67766 12.73049
## [185] 12.78051 12.82671 12.86804 12.90348 12.93199 12.95254 12.96724 12.97899
## [193] 12.98793 12.99418 12.99789 12.99918 12.99820 12.99508 12.98995 12.98294
## [201] 12.97420 12.96385 12.94963 12.92987 12.90564 12.87800 12.84802 12.81676
## [209] 12.78528 12.75465 12.72592 12.69522 12.65862 12.61714 12.57182 12.52366
## [217] 12.47369 12.42293 12.37240 12.32312 12.27611 12.23239 12.19299 12.15247
## [225] 12.10601 12.05557 12.00311 11.95056 11.89988 11.85303 11.81195 11.77859
## [233] 11.75059 11.72429 11.69976 11.67702 11.65614 11.63714 11.62008 11.60501
## [241] 11.59195 11.58097 11.57210 11.56540 11.56605 11.57679 11.59390 11.61370
## [249] 11.63250 11.64660 11.65232 11.65576 11.66452 11.67711 11.69205 11.70784
## [257] 11.72302 11.73607 11.74553 11.74990 11.74906 11.74443 11.73682 11.72706
## [265] 11.71595 11.70433 11.69299 11.68277 11.67447 11.66891 11.66692 11.66930
## [273] 11.67264 11.67358 11.67303 11.67187 11.67102 11.67136 11.67381 11.67927
## [281] 11.68862 11.70312 11.72226 11.74437 11.76777 11.79079 11.81175 11.82897
## [289] 11.84507 11.86347 11.88371 11.90529 11.92774 11.95059 11.97337 11.99558
## [297] 12.01677 12.03644 12.05413 12.06936 12.08341 12.09781 12.11245 12.12723
## [305] 12.14202 12.15671 12.17121 12.18539 12.19915 12.21237 12.22494 12.23676
## [313] 12.24771 12.25767 12.26699 12.27603 12.28474 12.29307 12.30096 12.30837
## [321] 12.31524 12.32153 12.32718 12.33208 12.33622 12.33968 12.34255 12.34490
## [329] 12.34682 12.34838
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")